/*
	会员等级修改 表单
*/
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

//dom
$(function(){
	layui.use(['layer', 'form', 'table'], function(){
		var layer = layui.layer, form = layui.form, table = layui.table;
		//ajaxData
		var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken };

		function getUserVipLevelData(level){
			/*
				获取用户vip等级标准信息
				接口方法 get
			*/
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend(ajaxData, {level: level || getParam('level')});
			$.ajax({
				url: BASE_SERVICEURL + URlS.vip,
				type: 'GET',
				data: _ajaxData,
				success: function(responseData){
					var code = responseData.msg.code;
					if(code == 200){
						//console.log(responseData)
						createFrom(responseData);
						layer.close(loadIndex)
					}else if(code == 2005){
						//登录失效
						layer.msg('登录失效，请重新登陆后重试', {time: 2000, end: function(){
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
						}});
					}else if(code == 2002){
						layer.msg('接口缺少必要参数，请确认数据均以填写后重试！', {time: 2000});
						layer.close(loadIndex)
					}
				},
				error: function(responseData){
					layer.msg(MESSAGES.service_error , { time: 2000, end: function(){
                    	window.location.href = './500.html';
                    }});
				}
			})
		};

		function modifyUserVipLevelData(levelData){
			/*
				修改用户vip等级信息
				接口方法 post
			*/
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend(ajaxData, levelData);

			$.ajax({
				url: BASE_SERVICEURL + URlS.vip,
				type: 'POST',
				data: _ajaxData,
				success: function(responseData){
					var code = responseData.msg.code;
					if(code == 200){
						layer.close(loadIndex)
						//console.log(responseData)
						layer.msg('修改成功!',{ time: 2000, end: function(){
							window.location.reload()
						}})
					}else if(code == 2005){
						//登录失效
						layer.msg('登录失效，请重新登陆后重试', {time: 2000, end: function(){
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
						}});
					}else if(code == 2002){
						layer.msg('接口缺少必要参数，请确认数据均以填写后重试！', {time: 2000});
						layer.close(loadIndex)
					}
				},
				error: function(responseData){
					layer.msg(MESSAGES.service_error , { time: 2000, end: function(){
                    	window.location.href = './500.html';
                    }});
				}
			})
		}
		// 构建表单
		function createFrom(responseData){
			//组织表格数据
			var data = responseData.data[0];
			var init_data = {
				vipLevel: data.level,
				nameCn: data.name['cn'],
				nameTw: data.name['tw'],
				nameEn: data.name['en'],
				standardCn: data.standard['cn'],
				standardTw: data.standard['tw'],
				standardEn: data.standard['en'],
			};
			form.val('vip-level-form', init_data);
        	form.render();
		}

		//修改
		form.on('submit(modify)', function(data){
			layer.confirm('确认修改用户vip等级信息？', {title:'提示'}, function(index){
				var f = data.field;
				//组织数据 并请求
				modifyUserVipLevelData({
					level: getParam('level') || f.vipLevel,
					name: langTransObj(f.nameCn, f.nameTw, f.nameEn),
					amount: langTransObj(f.standardCn, f.standardTw, f.standardEn)
				})
				layer.close(index);
			})
			return false;
		})


		getUserVipLevelData(getParam('level'));
	});
})
